
<?php
	class calendar{
		private $year,$month,$day;
		private $arrWeek=array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
		private $days;//月应有的天数
		private $arrMonth=array('1'=>'一月',
			'2'=>'二月',
			'3'=>'三月',
			'4'=>'四月',
			'5'=>'五月',
			'6'=>'六月',
			'7'=>'七月',
			'8'=>'八月',
			'9'=>'九月',
			'10'=>'十月',
			'11'=>'十一月',
			'12'=>'十二月',);
		/*设置年、月、日*/
		private function setYear($year){
			$this->year=$year;
		}
		private function setMonth($month){
			$this->month=$month;
		}
		private function setDay($day){
			$this->day=$day;
		}
		/*获取年、月、日*/
		private function getYear(){
			return $this->year;
		}
		private function getMonth(){
			return $this->month;
		}
		private function getDay(){
			return $this->day;
		}
		private function getDays($year,$month,$date=1){
				while(checkdate($month,$date,$year)){
					$date++;
				}
				return $date;
		}
		/*显示当前日期、或表单提交的日期*/
		public function showDate(){
				$this->_env();
				$nowday=$this->getWeek($this->getYear(),$this->getMonth(),$this->getDay());
				$firstday=$this->getWeek($this->getYear(),$this->getMonth(),1);
				$this->days=$this->getDays($this->getYear(),$this->getMonth());
				echo "<div style=\"text-align:center;font:9px;width:236;margin:0px 15px\"><form action=$_SERVER[PHP_SELF] method=post  style=\"\" name=calenderform id=calenderform>";
				echo "<select name=month id=month>";
				for($tmpi=1;$tmpi<13;$tmpi++){
					if($tmpi==$this->month){
						$select="selected style=\"background-color: #FFD700\"";
					}else{
						$select='';
					}
					echo "<option value=$tmpi $select>".$this->arrMonth[$tmpi]."</option>";
				}
				echo "</select><select name=year id=year>";
				for($tmpj=$this->year-15;$tmpj<=$this->year+15 && $tmpj<2038;$tmpj++){
					if($tmpj==$this->year){
						$select="selected style=\"background-color: #FFD700\"";
					}else{
						$select='';
					}
					echo "<option value=$tmpj $select>".$tmpj."</option>";
				}
				echo "</select>";
				echo "<table align=center><tr>";
				for($tmpi=0;$tmpi<count($this->arrWeek);$tmpi++){
					echo "<td width=29>".$this->arrWeek[$tmpi]."</td>";
				}
				echo "</tr>";
				$tmpj=1;
				while($tmpj < $this->days){
					if($tmpj==$this->day){
						$bgcolor='bgcolor=#FFD700';
					}else{
						$bgcolor='bgcolor=#FFE4B5';
					}
					if($tmpj==1){
					 echo "<tr>";
					  for($tmp=0;$tmp<$firstday;$tmp++){
						echo "<td ></td>";
					  }
					}
					if($this->getWeek($this->getYear(),$this->getMonth(),$tmpj)==0){
						echo "</tr><tr><td $bgcolor>$tmpj</td>";
					}else{
						echo "<td $bgcolor>$tmpj</td>";
					}
					$tmpj++;
					}
					/*if($this->getWeek($this->getYear(),$this->getMonth(),$this->days-1)!=6){
						for($i=$this->getWeek($this->getYear(),$this->getMonth(),$this->days-1);$i<=6;$i++){
							echo "<td $bgcolor></td>";
						}
					}*/
				echo "</tr></table></div>";
			}
				
		/*默设置认为系统的时间也可以自己设置时间*/
		private function _env(){
			if(!isset($_POST['year'])){
				$this->setYear(date('Y'));
			}else{
				$this->setYear($_POST['year']);
			}
			if(!isset($_POST['month'])){
				$this->setMonth(date('m'));
			}else{
				$this->setMonth($_POST['month']);
			}
			$this->setDay(date('d'));
		}
		/*获取星期*/
		private function getWeek($year,$month,$day){
			$strap=strtotime("$year-$month-$day");
			return date('w',$strap);
		}
	}
	$calendar=new calendar();
	$calendar->showDate();
?>